﻿
/*
 * Javasript based on Functions(JBF) 1.03
 * 
 * Copyright(c) 2010, Fooxee (JA86.CN)
 *
 * JBF is licensed under the terms of the Open Source GPL 3.0(http://www.gnu.org/licenses/gpl.html) license. 
 * 
 * JBF's blog: http://www.cnblogs.com/JBF/
 * 
 * Created by Fooxee. at 2010.03.08  mailTo:hjccjun@163.com
 */

/*

CSS类
CSSStyleDeclaration //'http://www.w3.org/TR/2000/REC-DOM-Level-2-Style-20001113'
操作模式:0兼容模式(默认) 1标准模式(效率高) 

*/

JBF.CSS=JBF.createClass({
						
	//CSS样式声明表
	styleDeclaration:{
		//盒子标签和属性
		'border'				:'border',
		'border-bottom'			:'borderBottom',
		'border-bottom-color'	:'borderBottomColor',
		'border-bottom-style'	:'borderBottomStyle',
		'border-bottom-width'	:'borderBottomWidth',
		'border-color'			:'borderColor',
		'border-left'			:'borderLeft',
		'border-left-color'		:'borderLeftColor',
		'border-left-style'		:'borderLeftStyle',
		'border-left-width'		:'borderLeftWidth',
		'border-right'			:'borderRight',
		'border-right-color'	:'borderRightColor',
		'border-right-style'	:'borderRightStyle',
		'border-right-width'	:'borderRightWidth',
		'border-style'			:'borderStyle',
		'border-top'			:'borderTop',
		'border-top-color'		:'borderTopColor',
		'border-top-style'		:'borderTopStyle',
		'border-top-width'		:'borderTopWidth',
		'border-width'			:'borderWidth',
		'clear'					:'clear',
		'float'					:'floatStyle',
		'margin'				:'margin',
		'margin-bottom'			:'marginBottom',
		'margin-left'			:'marginLeft',
		'margin-right'			:'marginRight',
		'margin-top'			:'marginTop',
		'padding'				:'padding',
		'padding-bottom'		:'paddingBottom',
		'padding-left'			:'paddingLeft',
		'padding-right'			:'paddingRight',
		'padding-top'			:'paddingTop',
		//颜色和背景标签和属性
		'background'			:'background',
		'background-attachment'	:'backgroundAttachment',
		'background-color'		:'backgroundColor',
		'background-image'		:'backgroundImage',
		'background-position'	:'backgroundPosition',
		'background-repeat'		:'backgroundRepeat',
		'color'					:'color',
		//样式标签和属性
		'display'				:'display',
		'list-style-type'		:'listStyleType',
		'list-style-image'		:'listStyleImage',
		'list-style-position'	:'listStylePosition',
		'list-style'			:'listStyle',
		'white-space'			:'whiteSpace',
		//文字样式标签和属性
		'font'					:'font',
		'font-family'			:'fontFamily',
		'font-size'				:'fontSize',
		'font-style'			:'fontStyle',
		'font-variant'			:'fontVariant',
		'font-weight'			:'fontWeight',
		//文本标签和属性
		'letter-spacing'		:'letterSpacing',
		'line-break'			:'lineBreak',
		'line-height'			:'lineHeight',
		'text-align'			:'textAlign',
		'text-decoration'		:'textDecoration',
		'text-indent'			:'textIndent',
		'text-justify'			:'textJustify',
		'text-transform'		:'textTransform',
		'vertical-align'		:'verticalAlign'
	},
	//构造方法
	init:function(el,mod)
	{
		if(!el)
		{
			JBF.debug(new Error(10,'元素[el]不能为空'));
			return;
		}
		this.mod = mod || 0;
		this.el=el;
		this.currentStyle=this.el.currentStyle || document.defaultView.getComputedStyle(this.el, null);
		
	},
	//添加css类名
	addClass:function(n)
	{
		if(JBF.isEmpty(this.el.className))
		{
			this.el.className=n;	
		}
		else
		{
			this.el.className=this.el.className+' '+n;
		}
	},
	////移除css类名
	removeClass:function(n)
	{
		if(!JBF.isEmpty(this.el.className))
		{
			var cnew='';
			JBF.each(this.el.className.split(' '),function(v,i)
			{
				if(v!=n)
					cnew += v+' ';
			});
			this.el.className=cnew;	
		}
	},
	//获取或设置css类名
	css:function(name)
	{
		if(!JBF.isEmpty(name))
		{
			//['class','class']
			if(JBF.isObject(name))
			{
				JBF.each(name,function(v,i)
				{
					if(v)
						this.addClass(v);
				},this);
			}
			//class class
			else if(JBF.isString(name))
			{
				JBF.each(name.split(' '),function(v,i)
				{
					if(v)
						this.addClass(v);
				},this);
			}
		}
		else
		{
			return 	this.el.className;
		}
	},
	//获取或设置style标签
	cssText:function(style)
	{
		if(!JBF.isEmpty(style))
		{
			//{css:'cssrule',css:'cssrule'}
			if(JBF.isObject(style))
			{
				JBF.each(style,function(v,i,list)
				{
					if(v)
						this.addStyle(v,list[v]);
				},this);
			}
			//css:'cssrule';css:'cssrule';
			else if(JBF.isString(style))
			{
				JBF.each(style.split(';'),function(v,i)
				{
					if(v)
						this.addStyle(v.split(':')[0],v.split(':')[1]);
				},this);
			}
		}
		else
		{
			return this.el.style.cssText;	
		}
	},
	//设置样式
	addStyle:function(n,v)
	{
		this.setStyle(n,v);
	},
	//清除样式
	removeStyle:function(n)
	{
		this.setStyle(n,'');
	},
	//private
	setStyle:function(n,v)
	{
		if(this.mod!=1)
		{
			var nn=this.styleDeclaration[n.toLowerCase()];
			if(!nn)
				nn=n;
			n=nn;
		}
		n=n.replace(/(^\s*)|(\s*$)/g, "");
		v=v.replace(/(^\s*)|(\s*$)/g, "");
		this.el.style[n]=v;
	}
});
